﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace MillerApp.WebApp.Ajax
{
    public partial class GetCallbackEventReference : System.Web.UI.Page, ICallbackEventHandler
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsCallback)
            {
                ClientScriptManager scriptManager = this.Page.ClientScript;
                string eventRef = scriptManager.GetCallbackEventReference(this, "argval", "OnCallComplete", "ctxval", "OnErrorOccurs", true);
                StringBuilder sb = new StringBuilder();

                sb.Append("function Invoke()");
                sb.Append(Environment.NewLine);
                sb.Append("{");
                sb.Append("var argval1=Math.ceil(Math.random()*100000);");
                sb.Append(Environment.NewLine);
                sb.Append("var argval2=Math.ceil(Math.random()*100000);");
                sb.Append(Environment.NewLine);
                sb.Append("var argval3=argval1+'='+argval2;");
                sb.Append(Environment.NewLine);
                sb.Append("Invoke2(argval3);");
                sb.Append(Environment.NewLine);
                sb.Append("}");

                sb.Append(Environment.NewLine);

                sb.Append("function Invoke2(argval,ctxval)");
                sb.Append(Environment.NewLine);
                sb.Append("{");
                sb.Append(eventRef);
                sb.Append("}");

                sb.Append(Environment.NewLine);

                sb.Append("function OnCallComplete(result,context)");
                sb.Append(Environment.NewLine);
                sb.Append("{");
                sb.Append("document.getElementById(\"TextBox1\").value = result;");
                sb.Append("}");

                sb.Append(Environment.NewLine);

                sb.Append("function OnErrorOccurs(error)");
                sb.Append(Environment.NewLine);
                sb.Append("{");
                sb.Append("document.getElementById(\"TextBox2\").value = error;");
                sb.Append("}");
                scriptManager.RegisterClientScriptBlock(base.GetType(), "CallBackScript", sb.ToString(), true);
            }
        }

        #region ICallbackEventHandler
        public string GetCallbackResult()
        {
           // throw new NotImplementedException();
            return System.DateTime.Now.ToString();
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            //throw new NotImplementedException();
        }
        #endregion ICallbackEventHandler

    }
}